// This cpp file is designed to provide an implementation for
// our character setup window
//
// Created by Christopher Bowlsby on 11/5/11

#include "createPlayer.h"
#include "ui_createplayer.h"
#include <cstdlib>
#include <ctime>

#include <iostream>

// constructor
createPlayer::createPlayer(QWidget *parent) :
    QDialog(parent), ui(new Ui::createPlayer) {
    ui->setupUi(this);

    // construct inital warrior and update GUI
    setPlayerClass("Warrior");
    setPlayerStats(17, 12, 5, 13);
}

// destructor
createPlayer::~createPlayer() {
    delete ui;
}


// this function gets the user's desired player name
// player object is only updated when the user hits "Ok"
void createPlayer::on_okButton_clicked() {
    // get character name and set it
    setPlayerName((ui->charName->text()).toStdString());
}


// this function creates the desired class and sets it up for use,
// player object is updated when the user selects their class
void createPlayer::on_classList_currentIndexChanged(int index) {
    // figure out class name and set class picture
    std::string temp;
    if(index == 0) {
        ui->classPic->setStyleSheet("background-image: url(./Data/warrior.png);");
        temp = "Warrior";
    }else if(index == 1) {
        ui->classPic->setStyleSheet("background-image: url(./Data/wizard.png);");
        temp = "Wizard";
    }else if(index == 2) {
        ui->classPic->setStyleSheet("background-image: url(./Data/ranger.png);");
        temp = "Ranger";
    }


    // set player class and update the gui
    setPlayerClass(temp);
    on_regenStats_clicked();
    ui->classPic->update();
}


// this function regenerates stats for a given class when the
// user does not like their starting stats, generated stats are applied
// to the player object each time they are generated
void createPlayer::on_regenStats_clicked() {
    // values hardcoded for now, until they can be dynamically
    // generated
    std::vector<int> stats = randNum();

    if(pclass == std::string("Warrior")) {
        ui->agility->setNum(stats.at(0));
        ui->attack->setNum(stats.at(1));
        ui->apoints->setNum(stats.at(2));
        ui->htpoints->setNum(stats.at(3));
    }else if(pclass == std::string("Wizard")) {
        ui->agility->setNum(stats.at(0));
        ui->attack->setNum(stats.at(1));
        ui->apoints->setNum(stats.at(2));
        ui->htpoints->setNum(stats.at(3));
    }else if(pclass == std::string("Ranger")) {
        ui->agility->setNum(stats.at(0));
        ui->attack->setNum(stats.at(1));
        ui->apoints->setNum(stats.at(2));
        ui->htpoints->setNum(stats.at(3));
    }

    // update GUI, then set player stats in createPlayer object
    ui->statsWidget->update();
    setPlayerStats(stats.at(0), stats.at(1), stats.at(2), stats.at(3));
}


// generates random number from 0 - 25 for stats usage
std::vector<int> createPlayer::randNum() {
    // initial variables
    std::vector<int> randNums;
    srand((unsigned)time(0));

    // generate random numbers
    for(int next = 0; next < 4; next++) {
        randNums.insert(randNums.begin() + next, rand() % 25);
    }

    // return vector of 4 random numbers for character class
    return randNums;
}


// setters and getters
void createPlayer::setPlayerName(std::string name) {
    pname = name;
}
std::string createPlayer::getPlayerName() const {
    return pname;
}
void createPlayer::setPlayerClass(std::string cclass) {
    pclass = cclass;
}
std::string createPlayer::getPlayerClass() const {
    return pclass;
}
void createPlayer::setPlayerStats(int ag, int at, int ap, int hp) {
    agility = ag;
    attack = at;
    apoints = ap;
    htpoints = hp;
}
std::vector<int> createPlayer::getPlayerStats() {
    std::vector<int> temp;
    temp.insert(temp.begin(), agility);
    temp.insert(temp.begin() + 1, attack);
    temp.insert(temp.begin() + 2, apoints);
    temp.insert(temp.begin() + 3, htpoints);
    return temp;
}
int createPlayer::getAgility() {
    return agility;
}
int createPlayer::getAttack() {
    return attack;
}
int createPlayer::getApoints() {
    return apoints;
}
int createPlayer::getHtpoints() {
    return htpoints;
}
